<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">
    <link rel="stylesheet" href="layui/css/layui.css" />
    <script type="text/javascript" src="layui/layui.js" ></script>
    <script type="text/javascript" src="js/jquery-1.12.2.min.js"></script>
</head>
<body>
<form class="layui-form" action="" lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">课程表</label>
        <div class="layui-input-block">
            <select name="subjectId" id="clazzNameHide" class="clazzName" lay-verify="required">
                <option value="">请选类别...</option>
            </select>
        </div>
    </div>

    <input type="hidden" name="id" lay-verify="id" class="layui-input">

    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="courseTitle" lay-verify="courseTitle" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入简介" class="layui-textarea" name="courseDesc" type=""></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addOne">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','layedit','laydate'],function () {
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        InitForm();

        //下拉初始化
        function InitForm(){
            $.ajax({
                type: 'post',
                url: 'subjectList.do',
                dataType: 'json',
                success: function(responseM){
                    if(responseM.subject.length > 0){
                        //加载班级
                        var clazzName = $("#clazzNameHide").val();
                        for(var i = 0;i < responseM.subject.length; i++){

                            var value = responseM.subject[i].id;
                            var name = responseM.subject[i].subjectName;
                            if(clazzName == value){
                                $(".clazzName").append('<option value="'+value+'" selected="">'+name+'</option>');
                            }else{
                                $(".clazzName").append('<option value="'+value+'">'+name+'</option>');
                            }
                        }
                        form.render('select');
                    }else{
                        layer.msg("下拉数据加载失败...");
                    }
                }
            })
        };

        form.on('submit(addOne)',function (data) {
            $.ajax({
                type:"post",
                url:"addCourse.do",
                data:data.field,
                dataType:"json",
                success:function (data) {
                    if (data.code == 1) {
                        layer.msg("添加成功!", {
                            icon: 6,
                            time: 500
                        }, function () {
                            var t = parent.layui.table;
                            t.reload('myCourse');// 重新加载页面,demo 表示父窗体table标签的id值
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        });
                    } else {
                        layer.msg(data.info);
                    }
                }
            });
            return false;
        });



    })
</script>

</body>
</html>